package com.fyt.utils;

import org.joor.Reflect;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;

@Component
public class build {
    public Object buildNoyInput(String code){  //无参
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            PrintStream printStream = new PrintStream(outputStream);
            PrintStream originalOut = System.out;
            System.setOut(printStream);
            Class<?> build = Reflect.compile("Main", code).type();
            Object instance = Reflect.on(build).create().get();
            Object result =  Reflect.on(instance).call("main",(Object)null).get();
            printStream.flush();
            System.setOut(originalOut);
            String output = outputStream.toString();
            return output;
    }
}
